package com.tuanzi.loan.business.service.system.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.system.FunctionalPermissionRelation;
import com.tuanzi.loan.business.entity.system.RelationType;
import com.tuanzi.loan.business.repository.system.FunctionalPermissionRelationRepository;
import com.tuanzi.loan.business.service.system.FunctionalPermissionRelationService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class FunctionalPermissionRelationServiceImpl extends BaseAuditServiceImpl<FunctionalPermissionRelation, String> implements FunctionalPermissionRelationService {

    @Autowired
    private FunctionalPermissionRelationRepository functionalPermissionRelationRepository;

    @Override
    protected JPAAuditRepository<FunctionalPermissionRelation, String> getRepository() {
        return functionalPermissionRelationRepository;
    }

    @Override
    public FunctionalPermissionRelation relate(String permissionId, RelationType relationType, String relationId) {
        FunctionalPermissionRelation functionalPermissionRelation = new FunctionalPermissionRelation();
        functionalPermissionRelation.setPermissionId(permissionId);
        functionalPermissionRelation.setRelationType(relationType);
        functionalPermissionRelation.setRelationId(relationId);
        return saveOrUpdate(functionalPermissionRelation);
    }

    @Override
    public void unrelate(String permissionId, RelationType relationType, String relationId) {
        FunctionalPermissionRelation functionalPermissionRelation = functionalPermissionRelationRepository.findByPermissionIdAndRelationTypeAndRelationId(permissionId,
                relationType, relationId);
        if (functionalPermissionRelation != null) {
            delete(functionalPermissionRelation);
        }
    }

    @Override
    public List<FunctionalPermissionRelation> findAllByRelationId(String relationId) {
        return functionalPermissionRelationRepository.findAllByRelationId(relationId);
    }

    @Override
    public List<FunctionalPermissionRelation> batchRelate(List<String> permissionIds, RelationType relationType, String relationId) {
        // 先清除原有数据
        functionalPermissionRelationRepository.deleteInBatch(functionalPermissionRelationRepository.findAllByRelationId(relationId));

        return permissionIds.parallelStream().map(x -> this.relate(x, relationType, relationId)).collect(Collectors.toList());
    }
}
